﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using yq.IBase;
using System.Data;
using yq.IBase.DBHelper;
using cn.Util;

namespace yq.IBase.Basis
{
    /// <summary>
    /// 水平切割数据库操作类
    /// 需要水平切割的表请继承这个类，并实现几个必须的方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class CutBusinessBase<T> : BusinessBase<T> where T : CutModelBase<T>, new() 
    {
        /// <summary>
        /// 添加,水平切割时ID应该通过GetNewsId()方法来生成
        /// </summary>
        /// <param name="info"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public override string Add(T info, DbHelper cn)
        {
            var currentT = new T();
            //获取表名
            string tableName = currentT.GetTableNameByCut(info);
            string keyValue = info.GetNewsId();
            info.SetKeyValue(keyValue);

            string SQL = currentT.GetInsertColumns(tableName);
            var comm = cn.GetSqlStringCommond(SQL);
            currentT.GetDbParameters(comm, info, cn);
            cn.ExecuteNonQuery(comm);

            //缓存操作
            cn.CaCheRefresh.Add<T>();

            return keyValue;

           // return Add(info, info.GetNewsId() , cn);
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="info"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public override int Update(T info, DbHelper cn)
        {
            //判断是否已加载
            if (info.IsLoad == false)
            {
                throw new Exception("未加载数据,不允许更新.");
            }
            var currentT = new T();

            //获取表名
            string tableName = currentT.GetTableNameByCut(info);

            ////重写版本号
            //info.SetVersionNewGuid();

            string sql = currentT.GetUpdateColumns(tableName);
            var comm = cn.GetSqlStringCommond(sql);
            currentT.GetDbParameters(comm, info, cn);

            //缓存操作
            cn.CaCheRefresh.Add<T>();

            return cn.ExecuteNonQuery(comm); ;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public override bool Delete(dynamic id, DbHelper cn)
        {
            var currentT = new T();
            //获取表名
            string tableName = currentT.GetTableNameByCut(new string(id));

            string sql = "Delete From " + currentT.GetTableName() + WhereStr + currentT.GetTableKey() + "=@Id";
            var comm = cn.GetSqlStringCommond(sql.ToString());
            System.Data.DbType type = SQLInParameter.GetDbType(id.GetType());
            cn.AddInParameter(comm, "@Id", type, id);
            int i = cn.ExecuteNonQuery(comm);

            //缓存操作
            cn.CaCheRefresh.Add<T>();

            return i != 0;
        }


        /// <summary>
        /// 传入sql语句分页查询 （自行控制）
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="page"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public override PageList<T> GetPageList(string sql,string orderBy, PageModel page, DbHelper cn)
        {
            return new CutSelectBase<T>(null).GetPageList(sql, orderBy, page, cn);
        }

        /// <summary>
        /// 查询条件（根据T推断表名）
        /// </summary>
        /// <returns></returns>
        public  SelectBase<T> Where(Expression<Func<T, bool>> func, T t)
        {
            CutSelectBase<T> select = new CutSelectBase<T>(t);
            select.Where(func);

            return select;
        }


        /// <summary>
        /// 查询条件 （根据T推断表名）
        /// </summary>
        /// <returns></returns>
        public  SelectBase<T> Where(T t)
        {
            return new CutSelectBase<T>(t);
        }

        /// <summary>
        /// 查询条件 （并行查询，注意效率）
        /// </summary>
        /// <returns></returns>
        public override SelectBase<T> Where(Expression<Func<T, bool>> func)
        {
            CutSelectBase<T> select = new CutSelectBase<T>(null);
            select.Where(func);

            return select;
        }


        /// <summary>
        /// 查询条件 （并行查询，注意效率）
        /// </summary>
        /// <returns></returns>
        public override SelectBase<T> Where()
        {
            return new CutSelectBase<T>(null);
        }

        /// <summary>
        /// 查询全部数据,并行查询请注意查询效率
        /// </summary>
        /// <param name="cn"></param>
        /// <returns></returns>
        public override IList<T> GetAllList(DbHelper cn)
        {
            return new CutSelectBase<T>(null).ToList(cn);
        }

        /// <summary>
        /// 水平分割不推荐使用这个方法。
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <param name="func"></param>
        /// <param name="list"></param>
        /// <param name="cn"></param>
        public override void Advance<K>(Func<T, ModelBase<K>> func, IList<T> list, DbHelper cn)
        {
            base.Advance<K>(func, list, cn);
        }
    }
}
